////////////////////////////////////////////
//  CColor.cpp : Utilisation de couleurs  //
//      Dans du texte en mode console     //
//           (ANSI Color Codes)           // 
////////////////////////////////////////////
//        Developpe par Soilwork          //
//              Version 0.9               //
//        Creation : 26/08/2004           //
//   Derniere Modification : 16/11/2004   //
////////////////////////////////////////////

#ifndef CCOLOR_CPP_INCLUDED
#define CCOLOR_CPP_INCLUDED

#include "CColor.h"

//Tableaux de codes de couleurs ANSI
//Code pour la couleur d'ecriture (Foreground)
string codeFG[9] = {"30","31","32","33","34","35","36","37","39"}; 
//Code pour l'arriere-plan (Background)
string codeBG[9] = {"40","41","42","43","44","45","46","47","49"};

/////////////////
//Constructeur //
/////////////////

CColor::CColor(int foreground, int background, int att)
{
  if(foreground != -1)
    if(foreground > 7 || foreground < 0)
      foreground = DEFAULT_COLOR;
  if(background != -1)
    if(background > 7 || background < 0)
      background = DEFAULT_COLOR;
  fgColor = foreground;
  bgColor = background;
  attributes = att;
}

CColor::CColor(const CColor &color)
{
  bgColor = color.getBGColor();
  fgColor = color.getFGColor();
  attributes = color.getAttributes();
}

void CColor::setFGColor(int color)
{
    if (color < 0 || color > 7)
        return;
    fgColor = color;
}    

void CColor::setBGColor(int color)
{
  if (color < 0 || color > 7)
    return;
  bgColor = color;
}

void CColor::setAttributes(int att)
{
  attributes=att;
}

void CColor::removeAttribute(int att)
{
  if (attributes & att) //Cet attribut est présent!
    attributes -= att; //On enlève l'attribut
}    

void CColor::resetDefault()
{
  attributes = -1;
  bgColor = DEFAULT_COLOR;
  fgColor = DEFAULT_COLOR;
}

void CColor::reset()
{
    attributes = -1;
    bgColor = -1;
    fgColor = -1;
}    

string CColor::getColorCode() const
{
  string result("\033[0");

  //les couleurs...
  if(fgColor != -1)
    result += ";" + codeFG[fgColor];

  if(bgColor != -1)
    result += ";" + codeFG[fgColor];

  if(attributes > 0)
    {    
      if(attributes & ATTRIB_BOLD)
	result += ";1";
      if(attributes & ATTRIB_ITALIC)
	result += ";3";
      if(attributes & ATTRIB_UNDERLINE)
	result += ";4";
      if(attributes & ATTRIB_BLINK)
	result += ";5";
      if(attributes & ATTRIB_REVERSE)
	result += ";7";
      if(attributes & ATTRIB_CONCEALED)
	result += ";8";
      if(attributes & ATTRIB_STRIKETHROUGH)
	result += ";9";
    } //attribute >= 0          
  //On ajoute le m final
  result += "m";
    return result;
}

string CColor::clearScreen() const
{
  return "\033[2J";
} 

#endif //CCOLOR_CPP_INCLUDED
